

window.onload=function () {

    init();
    let username="";

    document.getElementById("send").addEventListener("click",()=>{

        let target1= document.getElementById("compyname1");
        let target2=document.getElementById("compyname2");

        if(target1.value!=""&&target2.value!=""&&target1.value.length>=6&&target2.value.length>=6){


            if (XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            }

                let url="/addamd?name="+target1.value+"&pass="+target2.value;
                xhr.open("POST",url,true);
                xhr.send();
                xhr.onload=function () {

                    var tgd = xhr.responseText;
                    var status=JSON.parse(tgd).status;
                    if(status=="OK"){

                        //录入成功
                        alert("添加成功");
                    }else if (status=="HAVE") {

                        alert("用户名重复");
                    }else{

                        alert("保存失败");
                    }
                }
        }
});

    setTimeout(()=>{

        getInfo();

    },2000);


    function init() {

        if (XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        }

        let url="/adminfos";
        xhr.open("GET",url,true);
        xhr.send();

        xhr.onload=function () {

            var tgd = xhr.responseText;
            var data=JSON.parse(tgd).status;
            initLayout(data);

        }

    }

    function  initLayout(data) {

        if(data!=null){

            let pnode=document.getElementById("targetpop");
            let children=document.getElementById("children");
            children.style.opacity=1;

            children.children[1].innerHTML=data[0].name;

            if(data[0].name!=username){

                children.children[2].onclick=function () {

                    deleteInt(data[0]._id);
                }


            }

            for(let i=1;i<data.length;i++){

                let newnode=children.cloneNode(true);
                newnode.children[1].innerHTML=data[i].name;


                if(data[i].name!=username){

                    children.children[2].onclick=function () {

                        deleteInt(data[0]._id);
                    }

                }
            }

        }

    }


    function deleteInt(id) {



        if (XMLHttpRequest) {
            xhr = new XMLHttpRequest();

        }

        let url="/deleteadm?id="+id;
        xhr.open("GET",url,true);
        xhr.send();

        xhr.onload=function () {

            var tgd = xhr.responseText;
            var data=JSON.parse(tgd).status;
            initLayout(data);
        }

    }


    function getInfo() {

        if (XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        }

        let url="/getmyinfo";
        xhr.open("GET",url,true);
        xhr.send();

        xhr.onload=function () {

            var tgd = xhr.responseText;
            var data=JSON.parse(tgd).info;
            username=data;
            document.getElementById("ccname").innerHTML=data;

        }


    }
}